package java2d;

import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.contacts.Contact;

public class FirstTest {

    public static void main(String[] args) {
        World world = new World(new Vec2(0.0f, 0.0f));

        BodyDef bd1 = new BodyDef();
        bd1.linearDamping = ((float) 180 * 0.01f);
        bd1.angularDamping = ((float) 100 * 0.01f);
        bd1.awake = false;
        bd1.bullet = true;
        bd1.position = (new Vec2(-1.0f, 0.0f));
        bd1.type = BodyType.DYNAMIC;
        CircleShape shape = new CircleShape();
        shape.setRadius(0.5f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.restitution = ((float) 80 * 0.01f);
        fd.friction = ((float) 1 * 0.01f);
        fd.density = ((float) 1);


        Body jbox2dBody = world.createBody(bd1);
        jbox2dBody.createFixture(fd);

        jbox2dBody.setUserData("在最左边的英雄");


        BodyDef bd2 = new BodyDef();
        bd2.linearDamping = ((float) 180 * 0.01f);
        bd2.angularDamping = ((float) 100 * 0.01f);
        bd2.awake = false;
        bd2.bullet = true;
        bd2.position = (new Vec2(1.0f, 0.0f));
        bd2.type = BodyType.DYNAMIC;
        CircleShape shape1 = new CircleShape();
        shape1.setRadius(0.5f);
        FixtureDef fd2 = new FixtureDef();
        fd2.shape = shape1;
        fd2.restitution = ((float) 80 * 0.01f);
        fd2.friction = ((float) 1 * 0.01f);
        fd2.density = ((float) 1);

        Body jbox2dBody2 = world.createBody(bd2);
        jbox2dBody2.createFixture(fd2);

        jbox2dBody2.setUserData("在最右边的英雄");

        jbox2dBody2.setBullet(true);

        jbox2dBody.setLinearVelocity(new Vec2(10.0f, 0.0f));

        // 添加监听
        world.setContactListener(new ContactListener() {
            @Override
            public void beginContact(Contact contact) {
                System.out.println(contact.getFixtureA().getBody().getUserData());
                System.out.println(contact.getFixtureB().getBody().getUserData());

                contact.setEnabled(false);
            }

            @Override
            public void endContact(Contact contact) {

            }

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {

            }
        });

        for (int i = 0; i < 200; i++) {
            world.step(1f / 30, 6, 5);
            Vec2 vec2 = jbox2dBody.getLinearVelocity();
            System.out.println("bodyId=" + jbox2dBody2.getUserData() + ",step=" + i + ",vec2x=" + vec2.x + ",vec2.y=" + vec2.y + ",length=" + vec2.length());
        }

    }
}
